﻿#ifndef AUTOFOCUS1_H
#define AUTOFOCUS1_H

/**
 自动对焦
*/

#include <QtCore/QThread>
#include <QtCore/QList>
#include <QtCore/QVector>
#include <QtCore/QPoint>
#include <QtCore/QSemaphore>
#include "Camera/CameraCommon/CameraImage.h"
#include "StageControl/StageControlB140/StageControl.h"
#include "AutoFocus/FocusMeasure.h"
#include "Camera/CameraCommon/IndustryCamera.h"
#include <QTimer>
class AutoFocus1 : public QObject
{
    Q_OBJECT
public:

    enum STATE {IDLE, WAIT_MOVEUP_FINISH, WAIT_MOVEDOWN_FINISH, WAIT_STOP};
    AutoFocus1(QObject *parent = nullptr);
    void attach(Qly::CameraImage *image);
    void attach(StageControl *control);
    void attach(Qly::IndustryCamera * camera);
    void setRange(double lower_mm, double higher_mm, double speed); // 设置相对于当前位置的相对位移。
    double bestImage(QImage &image);
signals:
    void autoFocusFinished();
public slots:
    void startAutoFocus();
    void stop();
private slots:
    void setImage(QImage &RGB24Image);

    void zMoveFinished();
    void watchdogTimeout();
private:
    void startWatchDog(int ms = 0);
    void stopWatchDog();
    Qly::CameraImage * m_pCameraImage = nullptr;
    StageControl *m_pStageControl = nullptr;
    Qly::IndustryCamera * m_pCamera = nullptr;
    Qly::FocusMeasure m_focusMeasure;
    QImage m_bestImage;
    bool m_isFocus = false;
    double m_bestScore = 0.0;
    double m_low_mm = 0.0;
    double m_high_mm = 0.0;
    double m_speed = 1.0;
    double m_z_mm = 0.0;

    int m_count;
    int m_pos;
    STATE m_state = IDLE;
    QTimer *m_pWatchDog = nullptr;
};
#endif // AUTOFOCUS1_H
